package com.ddxz.middleground.master.controller;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.permission.PermissionInfo;
import com.ddxz.middleground.master.dto.permission.PermissionPage;
import com.ddxz.middleground.master.service.PermissionService;
import com.ddxz.tool.core.common.DdxzTree;
import com.ddxz.tool.web.annotation.DdxzRequestHeader;
import com.ddxz.tool.web.annotation.DdxzRequestParam;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/8/3
 */

@RestController
@Api(tags = "权限管理")
public class PermissionController {
    @ApiOperation(value = "权限检索", consumes = "application/json", produces = "application/json",
            notes = "按照指定的关键字和分页参数，检索权限清单")
    @RequestMapping(value = "/permission/list", method = RequestMethod.GET)
    public DdxzResponse permissionList(@DdxzRequestHeader MasterHeader header, @DdxzRequestParam PermissionPage page) {
        return service.permissionList(header, page);
    }

    @ApiOperation(value = "权限查看", consumes = "application/json", produces = "application/json",
            notes = "查看指定权限的详细信息")
    @RequestMapping(value = "/permission/{permissionId}/detail", method = RequestMethod.GET)
    public DdxzResponse permissionDetail(@DdxzRequestHeader MasterHeader header, @PathVariable(name = "permissionId") String perimssionId) {
        return service.permissionDetail(header, perimssionId);
    }

    @ApiOperation(value = "保存权限", consumes = "application/json", produces = "application/json",
            notes = "将权限信息保存到数据库中，如果id为null则是新增，否则为修改")
    @RequestMapping(value = "/permission/save", method = RequestMethod.POST)
    public DdxzResponse permissionSave(@DdxzRequestHeader MasterHeader header, @RequestBody PermissionInfo info) {
        return service.permissionSave(header, info);
    }

    @ApiOperation(value = "删除权限", consumes = "application/json", produces = "application/json",
            notes = "将权限信息从数据库中删除，如果有角色使用该权限则允许删除")
    @RequestMapping(value = "/permission/{permissionId}/remove", method = RequestMethod.DELETE)
    public DdxzResponse permissionRemove(@DdxzRequestHeader MasterHeader header, @PathVariable(name = "permissionId") String permissionId) {
        return service.permissionRemove(header, permissionId);
    }

    @ApiOperation(value = "权限绑定菜单", consumes = "application/json", produces = "application/json",
            notes = "将权限信息与具体的菜单进行绑定，如果权限已经绑定过则忽略；如果未绑定过则新绑定；如果原来绑定现在不绑定，则解绑")
    @RequestMapping(value = "/permission/{permissionId}/grant", method = RequestMethod.POST)
    public DdxzResponse permissionGrant(@DdxzRequestHeader MasterHeader header,
                                        @RequestBody List<DdxzTree> menuTree,
                                        @PathVariable("permissionId") String permissionId) {
        return service.permissionGrant(header, permissionId, menuTree);
    }

    private final PermissionService service;

    public PermissionController(PermissionService service) {
        this.service = service;
    }
}
